package controllers

import (
	"gitee.com/tsingeye/FreeEhome/config"
	"gitee.com/tsingeye/FreeEhome/tools/redisDB"
	"github.com/astaxie/beego"
)

//进行鉴权校验
type AuthController struct {
	beego.Controller
}

func (this *AuthController) Prepare() {
	controllerName, actionName := this.GetControllerAndAction()

	if controllerName != "SystemController" || actionName != "Login" {
		this.AuthToken()
	}
}

//token验证
func (this *AuthController) AuthToken() {
	token := this.Ctx.Request.Header.Get("Token")

	if !redisDB.Exists(token) {
		//token非法，校验失败
		this.Data["json"] = map[string]interface{}{
			"errCode": config.EHomeInvalidToken,
			"errMsg":  config.HTTPCodeMap[config.EHomeInvalidToken],
		}
		this.ServeJSON()
	}
}
